const path = require('path')
const fs = require('fs');
const { app } = require('electron');
const { promisify } = require('util');
const sleep = promisify(setTimeout);

const args = process.argv.slice(2); // 忽略前两个参数（'electron-forge' 和 'start'）
console.log('app start args:', args);

let homePath = path.dirname(app.getPath('exe')) // 获取到安装目录的文件夹名称
// let homePath = config.get('home_path');
if(isDev()){
    homePath = app.getAppPath();
}
if (!fs.existsSync(homePath)) {
    // If the directory doesn't exist, create it
    fs.mkdirSync(homePath, { recursive: true });
    // console.log('Directory created successfully.');
} else {
    // console.log('Directory already exists.');
}

function isDev () {
    return args[0] == 'dev';
}
function getHomePath(){
    return homePath;
}

function getAppPath(){
    return app.getAppPath();
}
// console.log(`homePath=${homePath}`)
// console.log(`appPath=${getAppPath()}`)

function getOsPlatform() {
    let osPlatform = process.platform == 'darwin' ? 'mac' : 'win';
    return osPlatform;
}

function isWindow(){
    return getOsPlatform() == 'win';
}

function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}


function generateBatchNumber() {
    const currentDate = new Date();
    const formattedDate = currentDate.toISOString().slice(0, 10).replace(/-/g, '');
    const randomPart = Math.floor(Math.random() * 10000); // 生成一个四位的随机数

    const batchNumber = formattedDate + randomPart;
    return batchNumber;
}

module.exports = {
    getOsPlatform,
    isWindow,
    getHomePath,
    getAppPath,
    sleep,
    getRandomInt,
    generateBatchNumber,
};
